package com.kpi.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.Exception;
import java.lang.String;
import java.util.LinkedList;
import java.util.List;
import com.kpijavatraining.module.utils.*;


@Controller
public class FirstController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(HttpServletRequest request, HttpSession session) {

        return "index";
    }

    @RequestMapping(value = "/process/", method = RequestMethod.POST)
    public String submitForm(HttpServletRequest request, String myvar) {

        return "registration";
    }

    @RequestMapping(value = "/registration/", method = RequestMethod.GET)
    public String submit(HttpServletRequest request) {

        return "registration";
    }

    @RequestMapping(value = "/authorization/", method = RequestMethod.POST)
    public String authorizer(HttpServletRequest request) {

        Authorizer authorizer = null;

        String login = request.getParameter("login");
        String password = request.getParameter("password");
        boolean isAuthorized = false;

        try{
            authorizer = new Authorizer();
            isAuthorized = authorizer.authorization(login,password);
        }catch (Exception e){

        }
        if(isAuthorized){
            return "Session";
        }

        return "authorizationError";
    }



    @RequestMapping(value = "/registration/", method = RequestMethod.GET)
    public String registration(HttpServletRequest request) {

        return "registration";
    }


    @RequestMapping(value = "/process/register/", method = RequestMethod.POST)
    public String regist(HttpServletRequest request) {


        String login = request.getParameter("login");
        String password = request.getParameter("password");

        Registrator registrator = null;

        boolean result = false;

        try{
            registrator = new Registrator();
            result = registrator.register(login,password);
        }catch (Exception e){

        }

        if(result){
            return "completeRegistration";
        }

        return "registerError";


    }






}
